/**
 * 点击表格，切换样式
 */
$(".grid-cell").live("click",function(){
	$(".grid-selected").removeClass("grid-selected");
	$(this).addClass("grid-selected");
});

/**
 * 点击可编辑金额的表格，则切换为编辑状态
 */
$(".grid-cell .edit").live("click",function(e){
	if(e.target.className == 'input-cell'){
		return false;
	}
	addFocus($(this));
	return false;
});

/**
 * 点击页面其他地方关闭编辑框
 */
$("body").click(function(e){
	if(e.target.className == 'input-cell'){
		return false;
	}
	removeFocus();
});

/**
 * 开启编辑框
 */
function addFocus(obj){
	removeFocus();
	var value = $(obj).attr("alt"); 
	$(obj).addClass("grid-edit");
	$(obj).children("div").remove();
	if(value!=0){
		$(obj).append("<input class='input-cell' value='"+value+"' type='text'/>");
	}else{
		$(obj).append("<input class='input-cell' type='text'/>");
	}
	$(obj).children(".input-cell").focus();
}

/**
 * 监听Tab和Enter，实现提交编辑
 */
$("#itemsContent").keydown(function(event){
	var keyCode = event.keyCode;
	if ( keyCode == 13 || keyCode == 9 ) { // 13-Enter ,9-Tab
		var obj = $(".grid-edit").nextAll(".edit");
		if(obj.length>0){
			addFocus($(".grid-edit").nextAll(".edit:eq(0)"));
			return false;
		}else{
			$(".grid-edit").parent().nextAll("tr").each(function(){
				if($(this).find(".edit").length>0){
					addFocus($(this).find(".edit")[0]);
					return false;
				}
			});
			return false;
		}
	}
});

/**
 * 关闭编辑框，记录编辑之后的数据
 */
function removeFocus(){
	$(".grid-edit").each(function(){
		var value = $(this).children(".input-cell:eq(0)").val();
		if(!value){
			value = 0;
		}
		$(this).attr("alt",value);
		if($(this).hasClass("money")){
			$(this).append("<div>"+changeTwoDecimal(value)+"</div>");
		}else{
			$(this).append("<div>"+changeIntNaN(value)+"</div>");
		}
		$(this).children(".input-cell").remove();
		$(this).removeClass("grid-edit");
		var id = $(this).parent().attr("id");
		var name = $(this).attr("name");
		var parentId = $(this).parent().attr("name");
		if(id in itemsUpdateList){
			itemsUpdateList[id][name] = value;   		 
	    }else{
	    	var side = $(this).parent().children("td[name='side']").attr("alt");
	    	var beginBalanceInit = changeNaN($(this).parent().children("td[name='beginBalanceInit']").attr("alt"));
	    	var jFYthDeditInit = changeNaN($(this).parent().children("td[name='jFYthDeditInit']").attr("alt"));
	    	var dFYthCreditInit = changeNaN($(this).parent().children("td[name='dFYthCreditInit']").attr("alt"));
	    	var ythBeginBalanceInit = changeNaN($(this).parent().children("td[name='ythBeginBalanceInit']").attr("alt"));
	    	var beginNumberInit = changeNaN($(this).parent().children("td[name='beginNumberInit']").attr("alt"));
	    	var jFYthNumberInit = changeNaN($(this).parent().children("td[name='jFYthNumberInit']").attr("alt"));
	    	var dFYthNumberInit = changeNaN($(this).parent().children("td[name='dFYthNumberInit']").attr("alt"));
	    	var ythBeginNumberInit = changeNaN($(this).parent().children("td[name='ythBeginNumberInit']").attr("alt"));
	    	var type = 1;
	    	if($(this).parent().hasClass("itemsBalance")){
	    		type = 2;
	    	}
	    	itemsUpdateList[id] = {
	    		type:type,
    			side:side,
    			beginBalanceInit:beginBalanceInit,
    			jFYthDeditInit:jFYthDeditInit,
    			dFYthCreditInit:dFYthCreditInit,
    			ythBeginBalanceInit:ythBeginBalanceInit,
    			beginNumberInit:beginNumberInit,
    			jFYthNumberInit:jFYthNumberInit,
    			dFYthNumberInit:dFYthNumberInit,
    			ythBeginNumberInit:ythBeginNumberInit
	    	};
       	}
		var tb = 0.00;
		if(itemsUpdateList[id].side=='0'){
			tb = parseFloat(itemsUpdateList[id].beginBalanceInit)-parseFloat(itemsUpdateList[id].jFYthDeditInit)+parseFloat(itemsUpdateList[id].dFYthCreditInit);
		}else{
			tb = parseFloat(itemsUpdateList[id].beginBalanceInit)+parseFloat(itemsUpdateList[id].jFYthDeditInit)-parseFloat(itemsUpdateList[id].dFYthCreditInit);
		}
		$(this).parent().children("td[name='ythBeginBalanceInit']").attr("alt",changeIntNaN(tb));
		$(this).parent().children("td[name='ythBeginBalanceInit']").text(changeTwoDecimal(tb));
		itemsUpdateList[id].ythBeginBalanceInit = tb;
		if($(this).parent().hasClass("itemsBalance")){
			updateItemsByItemsAssistant(parentId);
		}else{
			updateParent(parentId);
		}
	});
}

/**
 * 更新父科目的数据
 * @param parentId
 * @returns {Boolean}
 */
function updateParent(parentId){
	if(parentId!=""&&parentId!=null&&parentId!="undefined"){
		var bbs = 0;
		var jfs = 0;
		var dfs = 0;
		var ybbs = 0;
		var bns = 0;
		var jfns = 0;
		var dfns = 0;
		var ybns = 0;
		var side = $("#"+parentId).children("td[name='side']").attr("alt");
		$("tr[name='"+parentId+"']").each(function(){
			jfs = parseFloat(jfs) + changeNaN(parseFloat($(this).children("td[name='jFYthDeditInit']").attr("alt")));
			dfs = parseFloat(dfs) + changeNaN(parseFloat($(this).children("td[name='dFYthCreditInit']").attr("alt")));
			jfns = parseInt(jfns) + changeNaN(parseInt($(this).children("td[name='jFYthNumberInit']").attr("alt")));
			dfns = parseInt(dfns) + changeNaN(parseInt($(this).children("td[name='dFYthNumberInit']").attr("alt")));
			if(side==$(this).children("td[name='side']").attr("alt")){
				bbs = parseFloat(bbs) + changeNaN(parseFloat($(this).children("td[name='beginBalanceInit']").attr("alt")));
				bns = parseInt(bns) + changeNaN(parseInt($(this).children("td[name='beginNumberInit']").attr("alt")));
				ybns = parseInt(ybns) + changeNaN(parseInt($(this).children("td[name='ythBeginNumberInit']").attr("alt")));
			}else{
				bbs = parseFloat(bbs) - changeNaN(parseFloat($(this).children("td[name='beginBalanceInit']").attr("alt")));
				bns = parseInt(bns) - changeNaN(parseInt($(this).children("td[name='beginNumberInit']").attr("alt")));
				ybns = parseInt(ybns) - changeNaN(parseInt($(this).children("td[name='ythBeginNumberInit']").attr("alt")));
			}
		});
		if(side=='0'){
			ybbs = parseFloat(bbs) - parseFloat(jfs) + parseFloat(dfs);
		}else{
			ybbs = parseFloat(bbs) + parseFloat(jfs) - parseFloat(dfs);
		}
		$("#"+parentId).children("td[name='beginBalanceInit']").attr("alt",bbs);
		$("#"+parentId).children("td[name='jFYthDeditInit']").attr("alt",jfs);
		$("#"+parentId).children("td[name='dFYthCreditInit']").attr("alt",dfs);
		$("#"+parentId).children("td[name='ythBeginBalanceInit']").attr("alt",ybbs);
		$("#"+parentId).children("td[name='beginBalanceInit']").text(changeTwoDecimal(bbs));
		$("#"+parentId).children("td[name='jFYthDeditInit']").text(changeTwoDecimal(jfs));
		$("#"+parentId).children("td[name='dFYthCreditInit']").text(changeTwoDecimal(dfs));
		$("#"+parentId).children("td[name='ythBeginBalanceInit']").text(changeTwoDecimal(ybbs));
		$("#"+parentId).children("td[name='beginNumberInit']").attr("alt",bns);
		$("#"+parentId).children("td[name='jFYthNumberInit']").attr("alt",jfns);
		$("#"+parentId).children("td[name='dFYthNumberInit']").attr("alt",dfns);
		$("#"+parentId).children("td[name='ythBeginNumberInit']").attr("alt",ybns);
		$("#"+parentId).children("td[name='beginNumberInit']").text(changeIntNaN(bns));
		$("#"+parentId).children("td[name='jFYthNumberInit']").text(changeIntNaN(jfns));
		$("#"+parentId).children("td[name='dFYthNumberInit']").text(changeIntNaN(dfns));
		$("#"+parentId).children("td[name='ythBeginNumberInit']").text(changeIntNaN(ybns));
		updateParent($("#"+parentId).attr("name"));
	}else{
		return false;
	}
}

/**
 * 更新辅助核算科目的会计科目的数据
 * @param parentId
 * @returns {Boolean}
 */
function updateItemsByItemsAssistant(parentId){
	if(parentId!=""&&parentId!=null&&parentId!="undefined"){
		var bbs = 0;
		var jfs = 0;
		var dfs = 0;
		var ybbs = 0;
		var bns = 0;
		var jfns = 0;
		var dfns = 0;
		var ybns = 0;
		var side = $("#"+parentId).children("td[name='side']").attr("alt");
		$("tr[name='"+parentId+"']").each(function(){
			jfs = parseFloat(jfs) + changeNaN(parseFloat($(this).children("td[name='jFYthDeditInit']").attr("alt")));
			dfs = parseFloat(dfs) + changeNaN(parseFloat($(this).children("td[name='dFYthCreditInit']").attr("alt")));
			jfns = parseFloat(jfns) + changeNaN(parseInt($(this).children("td[name='jFYthNumberInit']").attr("alt")));
			dfns = parseFloat(dfns) + changeNaN(parseInt($(this).children("td[name='dFYthNumberInit']").attr("alt")));
			bbs = parseFloat(bbs) + changeNaN(parseFloat($(this).children("td[name='beginBalanceInit']").attr("alt")));
			bns = parseFloat(bns) + changeNaN(parseInt($(this).children("td[name='beginNumberInit']").attr("alt")));
			ybns = parseInt(ybns) + changeNaN(parseInt($(this).children("td[name='ythBeginNumberInit']").attr("alt")));
		});
		alert(ybns);
		if(side=='0'){
			ybbs = parseFloat(bbs) - parseFloat(jfs) + parseFloat(dfs);
		}else{
			ybbs = parseFloat(bbs) + parseFloat(jfs) - parseFloat(dfs);
		}
		$("#"+parentId).children("td[name='beginBalanceInit']").attr("alt",bbs);
		$("#"+parentId).children("td[name='jFYthDeditInit']").attr("alt",jfs);
		$("#"+parentId).children("td[name='dFYthCreditInit']").attr("alt",dfs);
		$("#"+parentId).children("td[name='ythBeginBalanceInit']").attr("alt",ybbs);
		$("#"+parentId).children("td[name='beginBalanceInit']").text(changeTwoDecimal(bbs));
		$("#"+parentId).children("td[name='jFYthDeditInit']").text(changeTwoDecimal(jfs));
		$("#"+parentId).children("td[name='dFYthCreditInit']").text(changeTwoDecimal(dfs));
		$("#"+parentId).children("td[name='ythBeginBalanceInit']").text(changeTwoDecimal(ybbs));
		$("#"+parentId).children("td[name='beginNumberInit']").attr("alt",bns);
		$("#"+parentId).children("td[name='jFYthNumberInit']").attr("alt",jfns);
		$("#"+parentId).children("td[name='dFYthNumberInit']").attr("alt",dfns);
		$("#"+parentId).children("td[name='ythBeginNumberInit']").attr("alt",ybns);
		$("#"+parentId).children("td[name='beginNumberInit']").text(changeIntNaN(bns));
		$("#"+parentId).children("td[name='jFYthNumberInit']").text(changeIntNaN(jfns));
		$("#"+parentId).children("td[name='dFYthNumberInit']").text(changeIntNaN(dfns));
		$("#"+parentId).children("td[name='ythBeginNumberInit']").text(changeIntNaN(ybns));
		updateParent($("#"+parentId).attr("name"));
	}else{
		return false;
	}
}